package com.zds.u_converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @className:DateConverter
 * @date: 2025/1/16 下午3:41
 * @description:自定义日期转换器
 *  转换例如： http://localhost:8080/user/date?date=2011-01-04
 */
public class DateConverter implements Converter<String, Date> {

    /**
     * 将日期字符串转成日期对象
     * @param string
     * @return
     */
    @Override
    public Date convert(String string) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(string);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }
}
